package servlet;

import dao.DocumentDAO;
import model.Document;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class DownloadServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        /*PrintWriter out = resp.getWriter();
        DocumentDAO documentDAO = new DocumentDAO();
        if (req.getParameter("id") != null && req.getParameter("code") != null) {
            Document document = documentDAO.getDocumentById(Integer.valueOf(req.getParameter("id")));
            if (document != null && document.getCode().equals(req.getParameter("code"))) {
                String filePath = "D:\\FILES\\" + document.getIdDocument();
                File downloadFile = new File(filePath);
                FileInputStream fileInputStream = new FileInputStream(downloadFile);
                ServletContext servletContext = getServletContext();
                String mimeType = servletContext.getMimeType(filePath);
                if (mimeType == null) {
                    // set to binary type if MIME mapping not found
                    mimeType = "application/octet-stream";
                }
                //System.out.println(mimeType);
                resp.setContentType(mimeType);
                //resp.setContentType("application/force-download");
                resp.setContentLength((int) downloadFile.length());
                resp.setHeader("Content-Transfer-Encoding", "binary");
                resp.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", document.getDocumentName()));
                OutputStream outStream = resp.getOutputStream();

                byte[] buffer = new byte[4096];
                int bytesRead = -1;

                while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                    outStream.write(buffer, 0, bytesRead);
                }
                outStream.flush();
                fileInputStream.close();
                outStream.close();

            } else {
                out.print("Sorry! Your link is incorrect.");
            }
        } else {
            out.print("Sorry! Your link is incorrect.");
        }*/

        int id = Integer.valueOf(req.getParameter("id"));
        String code = req.getParameter("code");
        DocumentDAO documentDAO = new DocumentDAO();
        File file = new File("D:\\FILES\\" + id);
        Document document = documentDAO.getDocumentById(Integer.valueOf(req.getParameter("id")));
        InputStream fis = new FileInputStream(file);
        String mimeType = getServletContext().getMimeType(file.getAbsolutePath());
        resp.setCharacterEncoding("utf-8");
        resp.setContentType(mimeType != null ? mimeType : "application/octet-stream");
        resp.setContentLength((int)file.length());
        resp.setHeader("Content-Disposition", "attachment; filename=\"" + document.getDocumentName() + "\"");

        ServletOutputStream os = resp.getOutputStream();
        byte[] bufferData = new byte[1024];
        int read=0;
        while((read = fis.read(bufferData))!= -1){
            os.write(bufferData, 0, read);
        }
        os.flush();
        os.close();
        fis.close();
    }
}

